<template>
  <!-- 导航栏 -->
  <van-nav-bar
    title="详情描述"
    left-arrow
    @click-left="goBack"
    @click-right="showShare = true"
  >
    <template #right>
      <van-icon name="ellipsis" size="18" />
    </template>
  </van-nav-bar>

  <div id="info">
    <!-- 分享面板 -->
    <van-share-sheet
      v-model:show="showShare"
      title="立即分享给好友"
      :options="shareOptions"
    />
  </div>

  <div class="clear"></div>
  <!-- 商品轮播图 -->
  <div class="aui-m-slider">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="(item, index) in info.thumbs_list" :key="index">
        <van-image fit="cover" :src="item" />
      </van-swipe-item>
    </van-swipe>
  </div>
  <div class="clear"></div>
  <div class="detailbox">
    <h4>{{ info.name }}</h4>
    <div class="price">
      <em>￥</em>{{ info.price }}<text>库存{{ info.stock }}</text>
    </div>
  </div>
  <div class="clear"></div>
  <div class="allkbox"></div>
  <div class="clear"></div>
  <div class="detailbox_2">
    <van-tabs v-model:active="active" type="card" color="#ee0a24">
      <van-tab title="产品参数">
        <div v-html="info.content"></div>
      </van-tab>
      <van-tab title="商品评价">
        <div class="rate-header">
          <div class="rate-score">
            <h4>与描述相符</h4>
            <strong>{{ info.average }}</strong>
          </div>
          <div class="rate-graph">
            <van-rate v-model="info.average" readonly allow-half />
          </div>
        </div>
        <div class="rate-content">
          <div v-if="ratelist.length > 0">
            <div class="review-box" v-for="item in ratelist" :key="item.id">
              <div class="review-item">
                <div class="from-whom">
                  <div class="avatar">
                    <img :src="item.business.avatar_text" alt="" />
                  </div>
                  <div class="name">{{ item.business.nickname }}</div>
                </div>
                <div class="review-details">
                  <div class="content">{{ item.comment }}</div>
                  <div style="margin-top: 10px" v-if="item.proimg_text.length > 0">
                    <van-image
                      v-for="(items, index) in item.proimg_text"
                      :key="index"
                      width="4rem"
                      style="margin-right: 1rem"
                      height="4rem"
                      fit="cover"
                      :src="items"
                      @click="changeImg(items)"
                    />
                  </div>
                  <div class="time">{{ item.updatetime }}</div>
                </div>
              </div>
            </div>
          </div>
          <div v-else>
            <div class="rate-empty">该商品还没有人写评价呢</div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
  <!-- 底部动作栏 -->
  <van-action-bar>
    <van-action-bar-icon icon="chat-o" text="客服" @click="contact" />
    <van-action-bar-icon
      icon="cart-o"
      text="购物车"
      :badge="info.cartcount"
      @click="toCartIndex"
    />
    <van-action-bar-button type="warning" text="加入购物车" @click="toCartPage" />
    <van-action-bar-button type="danger" text="立即购买" @click="toConfirmPage" />
  </van-action-bar>
</template>
<script setup>
const { cookies } = useCookies();
const proid = ref(0);
const route = useRoute();
const router = useRouter();
const showShare = ref(false);
const shareOptions = ref([
  [
    { name: "微信", icon: "wechat" },
    { name: "朋友圈", icon: "wechat-moments" },
    { name: "微博", icon: "weibo" },
    { name: "QQ", icon: "qq" },
  ],
  [
    { name: "复制链接", icon: "link" },
    { name: "分享海报", icon: "poster" },
    { name: "二维码", icon: "qrcode" },
    { name: "小程序码", icon: "weapp-qrcode" },
  ],
]);
const active = ref(0);
const LoginUser = reactive(cookies.get("LoginUser") ? cookies.get("LoginUser") : {});
const info = ref({});
const ratelist = ref([]);
onMounted(() => {
  proid.value = route.query.proid ? route.query.proid : 0;
  getInfoData();
});
const goBack = () => {
  router.go(-1);
};
const getInfoData = async () => {
  let data = {
    busid: LoginUser.id,
    proid: proid.value,
  };
  let result = await Api.productInfo(data);
  if (result.code == 1) {
    info.value = result.data.productinfo;
    ratelist.value = result.data.ratelist;
  }
};
// 图片预览
const changeImg = (val) => {
  showImagePreview([val])
};
// 立即购买
const toConfirmPage = () => {
  router.push({
    path: "/product/cart/confirm",
    query: {
      proid: proid.value,
    },
  });
};
// 购物车列表
const toCartIndex = () => {
  router.push({
    path: "/product/cart/index",
    query: {
      proid: proid.value,
    },
  });
};

const contact = () => {
  showConfirmDialog({
    title: "拨打电话",
    message: "是否确认拨打官方客服热线?",
  })
    .then(() => {
      location.href = `tel:13512633441`;
      return;
    })
    .catch(() => {});
};
const toCartPage = async () => {
  if (!LoginUser || JSON.stringify(LoginUser) == "{}") {
    showNotify({
      type: "warning",
      message: "未登陆，请去登陆",
      onClose: () => {
        router.push("/business/base/login");
      },
    });
  } else {
    let data = {
      proid: proid.value,
      busid: LoginUser.id,
      nums: 1,
    };
    let result = await Api.cartAdd(data);
    if (result.code == 1) {
      getInfoData();
      showConfirmDialog({
        title: result.msg,
        message: "是否跳转到购物车？",
      })
        .then(() => {
          router.push("/product/cart/index");
        })
        .catch(() => {});
    } else {
      showNotify({ type: "danger", message: result.msg });
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .van-share-sheet__options {
  justify-content: center;
}
::v-deep .van-tabs__nav--card {
  margin: 0;
}
.rate-header {
  padding: 10px 0;
  display: inline-flex;
  border-bottom: 1px solid #c8c8c8;
  .rate-score {
    width: 75px;
    padding: 0 20px;
    border-right: 1px dotted #ddd;
    text-align: center;

    h4 {
      color: #404040;
      font-size: 12px;
      font-weight: 100;
    }

    strong {
      color: #f60;
      font-family: arial;
      font-size: 32px;
      line-height: 32px;
      margin: 0 2px;
    }

    p {
      padding-top: 1px;
      padding-left: 7px;
      text-align: left;
    }
  }

  .rate-graph {
    padding-left: 90px;
    padding-top: 22px;
    width: 450px;
  }
}

.rate-content {
  position: relative;
  padding: 10px 0;
  margin-bottom: 40px;

  .review-box {
    width: 100%;
    padding: 10px 0;
    // justify-content:space-between;
    border-bottom: 1px solid #e3e3e3;
  }
  .review-item {
    display: inline-flex;
    align-items: center;

    .from-whom {
      width: 90px;
      text-align: center;
      line-height: 1.5;
      min-width: 90px;

      .avatar {
        width: 50px;
        height: 50px;
        display: inline-flex;
        border-radius: 50%;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }

    .review-details {
      box-sizing: border-box;
      overflow: hidden;
      position: relative;
      zoom: 1;
      padding-right: 10px;

      .content {
        font-size: 14px;
        line-height: 1.4;
        color: #3f3f3f;
        word-wrap: break-word;
      }

      .time {
        font-size: 12px;
        color: #b0b0b0;
      }
    }
  }

  .rate-empty {
    text-align: center;
    height: 55px;
    line-height: 50px;
    color: #666;
  }
}
</style>
